home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / xsq_test.pro < prev    next >
Text File  |  1997-07-08  |  7KB  |  172 lines

  1. ;$Id: xsq_test.pro,v 1.4 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1994-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5. ;+
  6. ; NAME:
  7. ;       XSQ_TEST
  8. ;
  9. ; PURPOSE:
  10. ;       This function computes the chi-squared goodness-of-fit test
  11. ;       between observed frequencies and the expected frequencies of
  12. ;       a theoretical distribution. The result is a two-element vector
  13. ;       containing the chi-squared test statistic X2 and probability
  14. ;       of obtaining a value of X2 or greater.
  15. ;
  16. ; CATEGORY:
  17. ;       Statistics.
  18. ;
  19. ; CALLING SEQUENCE:
  20. ;       Result = XSQ_TEST(OBFREQ, EXFREQ)
  21. ;
  22. ; INPUTS:
  23. ;       OBFREQ:  An n-element vector of type integer, float or double
  24. ;                containing observed frequencies.
  25. ;
  26. ;       EXFREQ:  An n-element vector of type integer, float or double
  27. ;                containing expected frequencies.
  28. ;
  29. ; KEYWORD PARAMETERS:
  30. ;       OBCELL:  Use this keyword to specify a named variable which returns
  31. ;                a vector of observed frequencies used to formulate the chi-
  32. ;                squared test statistic. The elements of this vector are 
  33. ;                often refered to as the "cells" of the observed frequencies.
  34. ;                The length of this vector is determined by the length of 
  35. ;                EXCELL described below.
  36. ;
  37. ;       EXCELL:  Use this keyword to specify a named variable which returns 
  38. ;                a vector of expected frequencies used to formulate the chi-
  39. ;                squared test statistic. If each of the expected frequencies 
  40. ;                contained in the n-element input vector, EXFREQ, has a 
  41. ;                magnitude of 5 or greater, then this vector is identical to 
  42. ;                EXFREQ. If EXFREQ contains elements of magnitude less than 5,
  43. ;                adjacent expected frequencies are combined. The identical
  44. ;                combinations are performed on the corresponding elements of 
  45. ;                OBFREQ.  
  46. ;
  47. ;     RESIDUAL:  Use this keyword to specify a named variable which returns
  48. ;                a vector of signed differences between corresponding cells
  49. ;                of observed frequencies and expected frequencies.
  50. ;                RESIDUAL(i) = OBCELL(i) - EXCELL(i). The length of this 
  51. ;                vector is determined by the length of EXCELL described
  52. ;                above. 
  53. ;
  54. ; EXAMPLE:
  55. ;       Define the vectors of observed and expected frequencies.
  56. ;         obfreq = [2, 1, 4, 15, 10, 5, 3]
  57. ;         exfreq = [0.5, 2.1, 5.9, 10.3, 10.7, 7.0, 3.5]
  58. ;       Test the hypothesis that the given observed frequencies are
  59. ;       an accurate approximation to the expected frequency distribution.
  60. ;         result = $
  61. ;           xsq_test(obfreq, exfreq, obcell = obcell, excell = excell)
  62. ;       The result should be the two-element vector [3.05040, 0.383920].
  63. ;       Since the vector of expected frequencies contains elements of
  64. ;       magnitude less than 5, adjacent expected frequencies are combined
  65. ;       resulting in fewer cells. The identical combinations are performed
  66. ;       on the corresponding elements of observed frequencies.
  67. ;       The cells used to formulate the chi-squared test statistic are 
  68. ;       contained in the keyword parameters, obcell and excell.
  69. ;       They should contain the values, [7, 15, 10, 8] and 
  70. ;       [8.5, 10.3, 10.7, 10.5], respectively.
  71. ;       The computed value of 0.383920 indicates that there is no reason to
  72. ;       reject the proposed hypothesis at the 0.05 significance level.
  73. ;
  74. ; PROCEDURE:
  75. ;       XSQ_TEST computes chi-squared goodness-of-fit test between observed 
  76. ;       frequencies and the expected frequencies of a theoretical distribution.
  77. ;       Expected frequencies of magnitude less than 5 are combined with 
  78. ;       adjacent elements resulting in a reduction of cells used to formulate
  79. ;       the chi-squared test statistic. If the observed frequencies differ 
  80. ;       significantly from the expected frequencies, the chi-squared test 
  81. ;       statistic will be large and the fit is poor. This situation requires 
  82. ;       the rejection of the hypothesis that the given observed frequencies 
  83. ;       are an accurate approximation to the expected frequency distribution. 
  84. ;
  85. ; REFERENCE:
  86. ;       PROBABILITY and STATISTICS for ENGINEERS and SCIENTISTS (3rd edition)
  87. ;       Ronald E. Walpole & Raymond H. Myers
  88. ;       ISBN 0-02-424170-9
  89. ;
  90. ; MODIFICATION HISTORY:
  91. ;       Written by:  GGS, RSI, August 1994
  92. ;-
  93.  
  94. pro freq_cell, obfreq, exfreq
  95.   ;Combine elements of the expected frequency that are less than 5.
  96.   ;Make corresponding changes to the vector of observed frequencies.
  97.   ief = where(exfreq lt 5, nex)
  98.   if nex ne 0 then begin
  99.     while nex ne 0 do begin
  100.       nfreq = n_elements(exfreq)
  101.       if exfreq[ief[0]] eq exfreq[0] then begin
  102.         ;First element less than 5.
  103.         exfreq[1] = exfreq[0] + exfreq[1]
  104.         exfreq = exfreq[1:*]
  105.         obfreq[1] = obfreq[0] + obfreq[1]
  106.         obfreq = obfreq[1:*]
  107.       endif else if exfreq[ief[0]] eq exfreq[nfreq-1] then begin
  108.         ;Last element less than 5.
  109.         exfreq[nfreq-2] = exfreq[nfreq-2] + exfreq[nfreq-1]
  110.         exfreq = exfreq[0:nfreq-2]
  111.         obfreq[nfreq-2] = obfreq[nfreq-2] + obfreq[nfreq-1]
  112.         obfreq = obfreq[0:nfreq-2]
  113.       endif else begin
  114.         ;Some middle element less than 5.
  115.         exfreq[ief[0]] = exfreq[ief[0]] + exfreq[ief[0]+1]
  116.         obfreq[ief[0]] = obfreq[ief[0]] + obfreq[ief[0]+1]
  117.         if ief[0] ne nfreq-2 then begin ;Second to last element?
  118.           exfreq = [ exfreq[0:ief[0]], exfreq[ief[0]+2:*] ]
  119.           obfreq = [ obfreq[0:ief[0]], obfreq[ief[0]+2:*] ]
  120.         endif else begin 
  121.           exfreq = [ exfreq[0:ief[0]] ]
  122.           obfreq = [ obfreq[0:ief[0]] ]
  123.         endelse
  124.       endelse
  125.       ief = where(exfreq lt 5, nex)
  126.     endwhile
  127.   endif
  128. end
  129.  
  130. function xsq_test, obfreq, exfreq, excell = excell, obcell = obcell, $
  131.                                    residual = residual
  132.  
  133.   on_error, 2
  134.   nex = n_elements(exfreq)
  135.   if nex ne n_elements(obfreq) then $
  136.     message, 'Observed and expected frequencies must be n-element vectors.
  137.  
  138.   ineg = where(obfreq lt 0, nneg)
  139.   if nneg ne 0 then message, $
  140.     'Vector of observed frequencies cannot contain negative data.'
  141.  
  142.   if total(exfreq) lt 5 then $
  143.     message, 'Total of expected frequencies must be 5 or greater.'
  144.  
  145.   iex = where(exfreq lt 5, nex)
  146.   if nex ne 0 then begin
  147.   ;Combine adjacent elements of expected frequency vector that are 
  148.   ;less than 5. Corresponding changes are made to adjacent elements 
  149.   ;of the observed frequency vector.
  150.     obcell = obfreq 
  151.     excell = exfreq
  152.     freq_cell, obcell, excell
  153.     ;Adjust degrees of freedom
  154.     df = n_elements(excell)-1
  155.     ;Chi-square test statistic.
  156.     residual = (obcell - excell)
  157.     z = total(residual^2.0 / excell)
  158.     ;Probability of obtaining a value of z or larger
  159.     prob = 1 - chisqr_pdf(z, df)
  160.     return, [z, prob]
  161.   endif else begin
  162.     obcell = obfreq
  163.     excell = exfreq
  164.     ;Degrees of freedom
  165.     df = n_elements(excell)-1
  166.     residual = (obcell - excell)
  167.     z = total(residual^2.0 / excell)
  168.     prob = 1 - chisqr_pdf(z, df)
  169.     return, [z, prob] 
  170.   endelse
  171. end
  172.